package com.gx.server.model.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gx.server.mapper.ExamCollectMapper;
import com.gx.server.mapper.ExamQuestionMapper;
import com.gx.server.model.domain.ExamCollect;
import com.gx.server.model.domain.ExamQuestion;
import com.gx.server.model.service.ExamCollectService;
import com.gx.server.model.service.ExamQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Dobby
 * @version 1.0
 */

@Service
public class ExamCollectServiceImpl  extends ServiceImpl<ExamCollectMapper, ExamCollect> implements ExamCollectService {


    @Autowired
    ExamQuestionMapper examQuestionMapper;

    @Override
    public String add_collect(Long id, int Collect_id) {
        int i = this.CollectByUserId(id, Collect_id);
        if (i>0) return "已有题目";

        ExamCollect examCollect = new ExamCollect();
        examCollect.setUserId(id);
        examCollect.setQuestionId(Collect_id);
        boolean save = this.save(examCollect);

        if (save){
            return "已收藏";
        }else {
            return "收藏失败";

        }

    }

    @Override
    public Boolean del_collect(Long id, int Collect_id) {
        LambdaQueryWrapper<ExamCollect> examCollectLambdaQueryWrapper = new LambdaQueryWrapper<ExamCollect>();
        examCollectLambdaQueryWrapper.eq(ExamCollect::getUserId,id);
        examCollectLambdaQueryWrapper.eq(ExamCollect::getQuestionId,Collect_id);
        return this.remove(examCollectLambdaQueryWrapper);
    }

    @Override
    public List<ExamQuestion> all_collect(Long id) {
        LambdaQueryWrapper<ExamCollect> examCollectLambdaQueryWrapper = new LambdaQueryWrapper<ExamCollect>();
        examCollectLambdaQueryWrapper.eq(ExamCollect::getUserId,id);
        List<ExamCollect> list = this.list(examCollectLambdaQueryWrapper);
        List<Integer> ids = new ArrayList<>();

        //题库id提取
        for (int i = 0; i < list.size(); i++) {
            ids.add(list.get(i).getQuestionId());
        }

        QueryWrapper<ExamQuestion> queryWrapper =new QueryWrapper<>();
        queryWrapper.select("*");
        queryWrapper.in("id", ids);
        List<ExamQuestion> examQuestions = examQuestionMapper.selectList(queryWrapper);

        return examQuestions;
    }

    @Override
    public int CollectByUserId(Long id, int questuinId) {
        LambdaQueryWrapper<ExamCollect> examCollectLambdaQueryWrapper = new LambdaQueryWrapper<ExamCollect>();
        examCollectLambdaQueryWrapper.eq(ExamCollect::getUserId,id);
        examCollectLambdaQueryWrapper.eq(ExamCollect::getQuestionId,questuinId);
        return this.count(examCollectLambdaQueryWrapper);
    }

    @Override
    public Boolean del_all_collect(Long id) {
        LambdaQueryWrapper<ExamCollect> examCollectLambdaQueryWrapper = new LambdaQueryWrapper<ExamCollect>();
        examCollectLambdaQueryWrapper.eq(ExamCollect::getUserId,id);
        return this.remove(examCollectLambdaQueryWrapper);
    }

}
